
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

class myCache {
    private Map<String,Object> map = new HashMap<>();
    private ReadWriteLock lock = new ReentrantReadWriteLock();
    // 放数据
    public void put(String Key,Object value){
        lock.writeLock().lock();;
        System.out.println(Thread.currentThread().getName()+" 写入"+ Key +" 号缓存中...");
        try {
            TimeUnit.MICROSECONDS.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        map.put(Key, value);
        System.out.println(Thread.currentThread().getName()+" 写入"+ Key +" 号缓存完成...");
        lock.writeLock().unlock();
    }
    // 取数据
    public void get(String Key){
        lock.readLock().lock();
        System.out.println(Thread.currentThread().getName()+" 读取"+ Key +" 号缓存中...");
        
        map.get(Key);
        System.out.println(Thread.currentThread().getName()+" 读取"+ Key +" 号缓存完成..."); 
        lock.readLock().unlock();
    }
    // 降级
    public void degrade(){
        lock.writeLock().lock();
        System.out.println("写锁加锁");
        lock.readLock().lock();
        System.out.println("读锁加锁");
        lock.writeLock().unlock();
        System.out.println("锁降级为读锁");
        lock.readLock().unlock();
        System.out.println("释放");
    }
    // 升级 无法实现
    public void upgrade(){
        lock.readLock().lock();
        System.out.println("读锁加锁");
        lock.writeLock().lock();
        System.out.println("锁升级为写锁");
        lock.writeLock().unlock();
        System.out.println("写锁释放");
        lock.readLock().unlock();
        System.out.println("读锁释放");
    }
}

public class ReadOrWrite{
    public static void main(String[] args) {
        myCache cache = new myCache();
        // for (int i = 0; i < 5; i++) {
        //     final int num = i;
        //     new Thread(()->{
        //         cache.put(num+"", num+"");
        //     },String.valueOf(i)).start();
        // }

        // for (int i = 0; i < 5; i++) {
        //     final int num = i;
        //     new Thread(()->{
        //         cache.get(num+"");
        //     },String.valueOf(i)).start();
        // }
        cache.degrade();
        FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                cache.upgrade();
                return "upgrade success!";
            }
        });
        new Thread(task).start();
        try {
            task.get(1000*2, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            
            e.printStackTrace();
        } catch (ExecutionException e) {
            
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.out.println("无法完成升级");
            e.printStackTrace();
        }

    }
}